#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

int main(int argc,char** argv)
{
  pid_t wpid,tpid;
  int i;
  for(i = 0;i < 5;i++)
  {
    sleep(i+1);
    wpid = fork();
    if(i == 2)
    {
      tpid = wpid;
    }
    if(wpid == 0)
    {
      printf("I am %d child,my pid is %d\n",i,getpid());
      return i + 1;
    }
  }
  int status;
  waitpid(tpid,&status,0);
  printf("I am parent,my child is %d\n",tpid);
  if(WIFEXITED(status))
  {
    printf("child return is %d\n",WEXITSTATUS(status));
  }

  return 0;
}
